package com.change.zh.ffmpegdo.push;

import java.io.IOException;

public class JobCommandExec {

    public JobCommandExec() {
    }

    private Runtime runtime = null;


    public JobDetail process(String id, String type, String command) {
        Process process = null;
        CommandConsoleThread consoleThread = null;
        JobDetail jobDetail = null;
        try {
            if (runtime == null) {
                runtime = Runtime.getRuntime();
            }
            System.out.println("JobCommandExec    :" + "执行命令：\n" + command);
            process = runtime.exec(command);// 执行本地命令获取任务主进程
            System.err.println("JobCommandExec    :" + "CommandConsoleThread开始打印执行结果");
            consoleThread = new CommandConsoleThread(process.getErrorStream(), id);
            consoleThread.start();
            System.err.println("JobCommandExec    :" + "开始装载jobDetail");
            jobDetail = new JobDetail(id, type, process, consoleThread);
            System.err.println("JobCommandExec    :" + "装载jobDetail成功");
        } catch (IOException e) {
            System.err.println("JobCommandExec    :" + "执行命令异常");
            stop(consoleThread);
            stop(process);
        }
        return jobDetail;
    }


    public boolean stop(Process process) {
        if (process != null) {
            process.destroy();
            return true;
        }
        return false;
    }

    @SuppressWarnings("deprecation")
    public boolean stop(CommandConsoleThread thread) {
        if (thread != null && thread.isAlive()) {
            thread.commandConsoleThreadDestroy();
            return true;
        }
        return false;
    }

}
